// pages/achivements/achivements.js
let app =  getApp();
// //背景音乐的地址
let musicUrl = "http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/audio/bgm.mp3";
let back =wx.getBackgroundAudioManager();//背景音乐控制器

Page({

    /**
     * Page initial data
     */
    data: {
        datas:[],//内含六张图片地址，就是那六个亮灭灯泡，在页面里直接调用即可
        isShow: false,//show
        cancel: -10000000,
        name:'',
        content:'',
        contents: [
            {
                content: '分享一次活动(未获得)',
                content_img:'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementoff.png',
                width: '173.48rpx',
                x: '0rpx',
                y: '0rpx'
            },
            {
                content: '通过济南六个校区(未获得)',
                content_img:'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementoff.png',
                width: '173.48rpx',
                x: '0rpx',
                y: '0rpx'
            },
            {
                content: '通过所有校区(未获得)',
                content_img:'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementoff.png',
                width: '173.48rpx',
                x: '0rpx',
                y: '0rpx'
            },
            {
                content: '连续答对5个问题(未获得)',
                content_img:'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementoff.png',
                width: '173.48rpx',
                x: '0rpx',
                y: '0rpx'
            },
            {
                
            },
            {
                content: '连续答对10个问题(未获得)',
                content_img:'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementoff.png',
                width: '173.48rpx',
                x: '0rpx',
                y: '0rpx'
            },
            {
                content: '隐藏成就，等待解锁中',
                content_img:'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/fglyoff.png',
                width: '173.48rpx',
                x: '0rpx',
                y: '0rpx'
            },
            {
                content: '隐藏成就，等待解锁中',
                content_img:'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/dghsoff.png',
                width: '173.48rpx',
                x: '0rpx',
                y: '0rpx'
            }
        ],
        
    },

    //背景音乐播放控制
    backmusic(){
        player();
        function player(){
          back.title = "成就页面";
          back.src = musicUrl;
          back.onEnded(() => {
            player();
          })
        }
    },

    show(e) {
        //音效
        let audio = wx.createInnerAudioContext();
        audio.src = "http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/audio/click.mp3"
        audio.play();

        // console.log(e);
        var that = this;
        that.setData({
            isShow: true,
            cancel: 1000000,
            name: e.currentTarget.dataset.name,
            content: that.data.contents[e.currentTarget.dataset.id].content,
        })
    },

    cancel(e) {
        //音效
        let audio = wx.createInnerAudioContext();
        audio.src = "http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/audio/click.mp3"
        audio.play();

        var that = this;
        that.setData({
            isShow: false,
            cancel: -10000000,
        })
    },

    /**
     * Lifecycle function--Called when page load
     */
    onLoad: function (options) {
        var that = this;
        //设置标题
        wx.setNavigationBarTitle({
            title: '成就',
        });
          
        //获取成就数据
        // that.backmusic();
        wx.request({
            url: app.globalData.urlHome + '/ach/all',
            data: {
                "openid": wx.getStorageSync('openid'),
            },
            header: {'content-type':'application/json'},
            method: 'POST',
            success: (result) => {
                that.setData({
                    datas: result.data,
                })
                
                if(result.data[0].light === 1) {
                    that.setData({
                        ['contents[' + 0 +'].content_img'] : 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementon.png',
                        ['contents[' + 0 +'].content']: '分享一次活动（已获得）',
                        ['contents[' + 0  +'].width']: '350rpx',
                        ['contents[' + 0 + '].x']: '-95rpx',
                        ['contents[' + 0 + '].y']: '-70rpx',
                    })
                }
                if(result.data[0].spring === 1) {
                    that.setData({
                        ['contents[' + 1 +'].content_img'] : 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementon.png',
                        ['contents[' + 1 +'].content']: '通过济南六个校区(已获得)',
                        ['contents[' + 1  +'].width']: '350rpx',
                        ['contents[' + 1 + '].x']: '-95rpx',
                        ['contents[' + 1 + '].y']: '-70rpx',
                    })
                }
                if(result.data[0].defender === 1) {
                    that.setData({
                        ['contents[' + 2 +'].content_img'] : 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementon.png',
                        ['contents[' + 2 +'].content']: '通过所有校区(已获得)',
                        ['contents[' + 2  +'].width']: '350rpx',
                        ['contents[' + 2 + '].x']: '-95rpx',
                        ['contents[' + 2 + '].y']: '-70rpx',
                    })
                }
                if(result.data[0].morning === 1) {
                    that.setData({
                        ['contents[' + 3 +'].content_img'] : 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementon.png',
                        ['contents[' + 3 +'].content']: '一次性回答正确全部问题(已获得)',
                        ['contents[' + 3  +'].width']: '350rpx',
                        ['contents[' + 3 + '].x']: '-95rpx',
                        ['contents[' + 3 + '].y']: '-70rpx',
                    })
                }
                if(result.data[0].wind === 1) {
                    that.setData({
                        ['contents[' + 5 +'].content_img'] : 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/achivementon.png',
                        ['contents[' + 5 +'].content']: '连续回答错误5次',
                        ['contents[' + 5  +'].width']: '350rpx',
                        ['contents[' + 5 + '].x']: '-95rpx',
                        ['contents[' + 5 + '].y']: '-70rpx',
                    })
                }
                if(result.data[0].careless === 1) {
                    that.setData({
                        ['contents[' + 6 +'].content_img'] : 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/fglyon.png',
                        ['contents[' + 6  +'].width']: '400rpx',
                        ['contents[' + 6 +'].content']: '连续答对10个题目(已获得)',
                        ['contents[' + 6 + '].x']: '-115rpx',
                        ['contents[' + 6 + '].y']: '-120rpx',
                    })
                }
                if(result.data[0].thunder === 1) {
                    that.setData({
                        ['contents[' + 7 +'].content_img'] : 'http://www.kynix.top:8080/fileManager-1.0-SNAPSHOT/image/sduhour/achivement/dghson.png',
                        ['contents[' + 7  +'].width']: '400rpx',
                        ['contents[' + 7 +'].content']: '连续回答错误5次(已获得)',
                        ['contents[' + 7 + '].x']: '-115rpx',
                        ['contents[' + 7 + '].y']: '-120rpx',
                    })
                }
            },
            fail: () => {},
            complete: () => {}
        });
          
    },

    /**
     * Lifecycle function--Called when page is initially rendered
     */
    onReady: function () {

    },

    /**
     * Lifecycle function--Called when page show
     */
    onShow: function () {
        var that = this;
        that.backmusic();
    },

    /**
     * Lifecycle function--Called when page hide
     */
    onHide: function () {
        back.pause();
    },

    /**
     * Lifecycle function--Called when page unload
     */
    onUnload: function () {

    },

    /**
     * Page event handler function--Called when user drop down
     */
    onPullDownRefresh: function () {

    },

    /**
     * Called when page reach bottom
     */
    onReachBottom: function () {

    },

    /**
     * Called when user click on the top right corner to share
     */
    onShareAppMessage: function () {

    }
})